﻿using FastORM.Attribute;
using FastORM.Entity;
using FastORM.Enum;
using FastORM.Mapping;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastORM.Realization
{
    public class MySQLBuild : DefaultSQLBuild
    {
        public override string BuildFindPageSQL<T>(string Columns, string Where, string OrderBy, int PageIndex, int PageSize)
        {
            MapperEntity map = StructMapping.GetInstance().GetMapperEntity(typeof(T));
            string sql = string.Format("select {0} from {1}", Columns, map.TableName);
            if (!string.IsNullOrEmpty(Where))
            {
                sql += string.Format(" where {0} ", Where);
            }
            if (!string.IsNullOrEmpty(OrderBy))
            {
                sql += string.Format(" order by {0} ", OrderBy);
            }
            sql += string.Format(" limit {0},{1}", (PageIndex - 1) * PageSize, PageSize);
            return sql;
        }

        public override string BuildFindPageSQL<T>(string SQL,int PageIndex, int PageSize)
        {
            SQL += string.Format(" limit {0},{1}", (PageIndex - 1) * PageSize, PageSize);
            return SQL;
        }
        public override string BuildFindPageSQL(string SQL, int PageIndex, int PageSize)
        {
            SQL += string.Format(" limit {0},{1}", (PageIndex - 1) * PageSize, PageSize);
            return SQL;
        }
    }
}
